﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Linq;

public partial class NavigationModeEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid NavigationModeID = Guid.Empty;
    NavigationModesManager manager = new NavigationModesManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    var bank = manager.Single(s => s.RecordID == RecordID);

                    txtName.Text = bank.Name;
                    txtBeginDate.DateValue = bank.BeginDate;
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            var CurrentUser = Session["User"] as UserHelper;

            Guid NewNavigationModeID = Guid.Empty;

            if (RecordID == Guid.Empty)
            {
                if (Guid.TryParse(Request.QueryString["sid"], out NavigationModeID))
                {
                    if (manager.Find(f => f.IsActive && f.NavigationModeID == NavigationModeID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("Regime already exists with this date");
                    }

                    manager.Add(new NavigationMode
                    {
                        RecordID = Guid.NewGuid(),
                        NavigationModeID = NavigationModeID,
                        Name = txtName.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    });

                    NewNavigationModeID = NavigationModeID;
                }
                else
                {
                    var navigationmode = new NavigationMode
                    {
                        RecordID = Guid.NewGuid(),
                        NavigationModeID = Guid.NewGuid(),
                        Name = txtName.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    };

                    manager.Add(navigationmode);

                    NewNavigationModeID = navigationmode.NavigationModeID;
                }
            }
            else
            {
                var navigationmode = manager.Single(s => s.RecordID == RecordID);

                if (manager.Find(f => f.IsActive && f.NavigationModeID == navigationmode.NavigationModeID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                {
                    throw new Exception("Regime already exists with this date");
                }

                navigationmode.Name = txtName.Text;
                navigationmode.BeginDate = txtBeginDate.DateValue.Value;
                navigationmode.LastModifyDate = DateTime.Now;
                navigationmode.LastModifier = CurrentUser.UserID;

                NewNavigationModeID = navigationmode.NavigationModeID;
            }

            manager.SaveChanges();
            manager.RepiarDates(NewNavigationModeID);

            if (NavigationModeID == Guid.Empty && RecordID == Guid.Empty)
            {
                Response.Redirect("navigationmodeview.aspx?m=liBanks");
            }
            else
            {
                Response.Redirect(String.Format("~/navigationmodehistory.aspx?id={0}&m={1}", NewNavigationModeID, "liNavigationMode"));
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}